home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / hsclib / entity.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  4.3 KB  |  225 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1995-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * entity.c
  22.  *
  23.  * entity structure, variables and functions ( "&xx;")
  24.  *
  25.  * updated: 13-Oct-1996
  26.  * created:  8-Sep-1995
  27.  *
  28.  */
  29.  
  30. #define NOEXTERN_HSCLIB_ENTITY_H
  31. #include "hsclib/inc_base.h"
  32. #include "hsclib/entity.h"
  33.  
  34. /*
  35.  *-------------------------------------
  36.  * constructor/destructor
  37.  *-------------------------------------
  38.  */
  39.  
  40. /*
  41.  * new_hscent
  42.  *
  43.  * alloc & init a new hscentity
  44.  */
  45. HSCENT *new_hscent(STRPTR newid)
  46. {
  47.     HSCENT *newent = (HSCENT *) umalloc(sizeof(HSCENT));
  48.  
  49. #if DEBUG_ENTITY
  50.     fprintf(stderr, DHL "   new_enty %s\n", newid);
  51. #endif
  52.  
  53.     if (newent)
  54.     {
  55.         /* init new entity item */
  56.         newent->name = strclone(newid);         /* set id */
  57.         newent->replace = NULL;
  58.         newent->numeric = 0L;
  59.     }
  60.  
  61.     return (newent);
  62.  
  63. }
  64.  
  65. /*
  66.  * del_entity
  67.  */
  68. VOID del_entity(APTR data)
  69. {
  70.     HSCENT *ent = (HSCENT *) data;
  71.  
  72. #if DEBUG_ENTITY
  73.     fprintf(stderr, DHL "   del_enty %s\n", ent->name);
  74. #endif
  75.  
  76.     ufreestr(ent->name);
  77.     ufreestr(ent->replace);
  78.     ent->numeric = 0;
  79.     ufree(ent);
  80. }
  81.  
  82. /*
  83.  * cpy_hscent
  84.  *
  85.  * create a copy of an already existing entity
  86.  */
  87. HSCENT *cpy_hscent(HSCENT * oldent)
  88. {
  89.     HSCENT *newent = new_hscent(oldent->name);
  90.  
  91.     if (newent)
  92.     {
  93.         newent->replace = strclone(oldent->replace);
  94.         newent->numeric = oldent->numeric;
  95.     }
  96.  
  97.     return (newent);
  98. }
  99.  
  100. /*
  101.  *---------------------------
  102.  * find entity string
  103.  *---------------------------
  104.  */
  105.  
  106. /*
  107.  * cmp_strent
  108.  *
  109.  * compares a entity-string with the name
  110.  * of a HSCENT-entry
  111.  */
  112. int cmp_strent(APTR cmpstr, APTR entdata)
  113. {
  114.     STRPTR entstr = NULL;
  115.  
  116.     if (entdata)
  117.         entstr = ((HSCENT *) entdata)->name;
  118.  
  119.     if (entstr)
  120.         if (!strcmp(cmpstr, entstr))
  121.             return (-1);
  122.         else
  123.             return (0);
  124.     else
  125.         return (0);
  126. }
  127.  
  128. /*
  129.  * cmp_nument
  130.  *
  131.  * compares a entity-string with the numeric
  132.  * data of a HSCENT-entry
  133.  */
  134. int cmp_nument(APTR cmpstr, APTR entdata)
  135. {
  136.     LONG num = -1;
  137.     LONG cmpnum = (LONG) cmpstr;
  138.  
  139.     if (entdata)
  140.         num = ((HSCENT *) entdata)->numeric;
  141.  
  142.     return ((num != -1) && (num == cmpnum));
  143. }
  144.  
  145. /*
  146.  * cmp_rplcent
  147.  *
  148.  * compares a entity-string with the replace-item
  149.  * of a HSCENT-entry
  150.  */
  151. int cmp_rplcent(APTR cmpstr, APTR entdata)
  152. {
  153.     STRPTR entstr = NULL;
  154.  
  155.     if (entdata)
  156.         entstr = ((HSCENT *) entdata)->replace;
  157.  
  158.     if (entstr)
  159.         if (!strcmp(cmpstr, entstr))
  160.             return (-1);
  161.         else
  162.             return (0);
  163.     else
  164.         return (0);
  165. }
  166.  
  167. /*
  168.  *-------------------------------------
  169.  * append entity functions
  170.  *-------------------------------------
  171.  */
  172.  
  173. /*
  174.  * app_entnode
  175.  *
  176.  * create a new entity and append it to entity-list
  177.  *
  178.  * params: entid..name of the new entity (eg "uuml")
  179.  * result: ptr to the new entity or NULL if no mem
  180.  */
  181. HSCENT *app_entnode(DLLIST * entlist, STRPTR entid)
  182. {
  183.     HSCENT *newent;
  184.  
  185.     newent = new_hscent(entid);
  186.     if (newent)
  187.     {
  188.         if (app_dlnode(entlist, newent) == NULL)
  189.         {
  190.             del_entity((APTR) newent);
  191.             newent = NULL;
  192.         }
  193.     }
  194.  
  195.     return (newent);
  196. }
  197.  
  198. /*
  199.  * add_ent
  200.  */
  201. BOOL add_ent(DLLIST * entlist, STRPTR entid, STRPTR entreplace, LONG num)
  202. {
  203.     HSCENT *newent = app_entnode(entlist, entid);
  204.  
  205.     if (entreplace)
  206.         newent->replace = strclone(entreplace);
  207.  
  208.     newent->numeric = num;
  209.  
  210. #if 0
  211.     DDE(
  212.            {
  213.            STRPTR rplc = entreplace;
  214.            if (!rplc)
  215.            rplc = "(empty)";
  216.            fprintf(stderr, DHL "defent: \"%s\" \"%s\" %ld\n",
  217.                    entid, rplc, num);
  218.            }
  219.     );
  220. #endif
  221.  
  222.     return (TRUE);
  223. }
  224.  
  225.